package com.heqiang.spring.core.io;

import cn.hutool.core.lang.Assert;
import com.heqiang.spring.beans.BeansException;
import com.heqiang.spring.util.ClassUtils;

import java.io.IOException;
import java.io.InputStream;

public class ClassPathResource implements Resource {

    private final String path;

    private ClassLoader classLoader;

    public ClassPathResource (String path) {
        this(path,(ClassLoader) null);
    }

    public ClassPathResource (String path,ClassLoader classLoader) {
        Assert.notNull(path,"path不能为空");
        this.path = path;
        this.classLoader = classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader();
    }

    @Override
    public InputStream getInputStream() throws IOException {
        InputStream inputStream = classLoader.getResourceAsStream(path);
        if (inputStream == null) {
            throw new BeansException("无文件流");
        }
        return inputStream;
    }
}
